package com.glq.book.servlet;

import com.glq.book.entity.Borrow;
import com.glq.book.entity.User;
import com.glq.book.service.BorrowService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/borrow")
public class BorrowServlet extends HttpServlet {
    private String type = "";
    private BorrowService borrowService = new BorrowService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            type = "findAll";
        }
        switch (type) {
            case "findAll":
                List<Borrow> list = borrowService.selectBorrowList();
                req.getSession().setAttribute("borrowList", list);
                //跳转页面
                resp.sendRedirect("modeBorrow.jsp");
                break;
            case "userBorrowList":
                String username = req.getParameter("username");
                list = borrowService.selectBorrowListByUser(username);
                req.getSession().setAttribute("borrowList", list);
                //跳转页面
                resp.sendRedirect("modeBorrowList.jsp");
                break;
            case "repay":
                String id = req.getParameter("id");
                String bookName = req.getParameter("bookName");
                boolean b = borrowService.updateBorrow(Integer.parseInt(id),bookName);
                if (b) {
                    User user = (User) req.getSession().getAttribute("userInfo");
                    //成功，跳转页面
                    resp.sendRedirect("/borrow?type=userBorrowList&username=" + user.getUsername());
                } else {
                    //失败，跳转错误页面
                    resp.getWriter().write("还书失败，请稍后再试");
                }
            default:
                //跳转错误页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            resp.sendRedirect("/borrow?type=findAll");
        }
        switch (type) {
            case "borrowBook":
                String bookName = req.getParameter("bookName");
                String username = req.getParameter("username");
                String bookId = req.getParameter("bookId");
                boolean b = borrowService.insertBorrow(bookName, username, Integer.valueOf(bookId));
                if (b) {
                    //成功，跳转页面
                    resp.getWriter().write("借书成功");
                } else {
                    //失败，跳转错误页面
                    resp.getWriter().write("fail");
                }
            default:
                //跳转错误页面
        }
    }
}
